package com.mms.action;

import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.mms.dao.PersonManagementDao;
import com.mms.entity.Department;
import com.mms.entity.Staff;
import com.mms.service.StaffService;
import com.mms.util.MyMeetingInfo;
import com.opensymphony.xwork2.Action;

public class LoginAction implements Action {

    private Integer departmentSe;
    private String userName;
    private Integer sex;
    private String phone;
    private String password;
    private String registerCode;
    private String name;
    private String pwd;
    private StaffService staffService;
    

    public String register() throws UnsupportedEncodingException {
        HttpServletRequest request=ServletActionContext.getRequest();
        request.setCharacterEncoding("UTF-8");
        Staff staff=new Staff();
        staff.setAdminFlag(0);
        Department department=staff.getDepartment();
        department.setDeId(departmentSe);
        staff.setDepartment(department);
        staff.setPassword(password);
        staff.setPhone(phone);
        staff.setPosition("普通职员");
        staff.setSName(userName);
        staff.setSex(sex);
        staff.setState(0);
        staff.setReCode(registerCode);
        if (staffService.registerStaff(staff)) {
            request.setAttribute("isRegisterSuccessful", "true");
            return SUCCESS;
        }
        request.setAttribute("isRegisterSuccessful", "false");
        return ERROR;
    }
    
    public String login() throws UnsupportedEncodingException {
        HttpServletRequest request=ServletActionContext.getRequest();
        request.setCharacterEncoding("UTF-8");
        
        int staffID=staffService.checkUser(name, pwd);
        System.out.println("id="+staffID);
        System.out.println("np="+name+" ---- "+pwd);
        
        if (staffID!=-1) {//密码正确
            if (staffService.validateState(staffID)) {//状态通过可以登录
                request.getSession().setAttribute("userName", name);
                request.getSession().setAttribute("userID", staffID);
                request.setAttribute("isAdmit", true);
                request.setAttribute("isLoginSuccessful", true);
                if (staffService.isAdminister(staffID)) 
                    request.getSession().setAttribute("adminFlag", "1");//为管理员
                else 
                    request.getSession().setAttribute("adminFlag", "0");
                request=MyMeetingInfo.getMyMeetingInfo(request);
                return SUCCESS;
            }
            else {//状态不为已审批不能通过
                request.setAttribute("isAdmit", false);
            }
                
            }
            else {//用户密码错误
                request.setAttribute("isLoginSuccessful", false);
                if (staffService.isUserExist(name)) {
                    request.setAttribute("userName", name);
                }
            }
        return ERROR;
        
    }
    
    public String logout() throws UnsupportedEncodingException {
        HttpServletRequest request=ServletActionContext.getRequest();
        request.setCharacterEncoding("UTF-8");
        request.getSession().setAttribute("userName", "");
        return SUCCESS;
    }
    
    public StaffService getStaffService() {
        return staffService;
    }

    public void setStaffService(StaffService staffService) {
        this.staffService = staffService;
    }
    
    public Integer getDepartmentSe() {
        return departmentSe;
    }


    public void setDepartmentSe(Integer departmentSe) {
        this.departmentSe = departmentSe;
    }
    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public Integer getSex() {
        return sex;
    }


    public void setSex(Integer sex) {
        this.sex = sex;
    }


    public String getPhone() {
        return phone;
    }


    public void setPhone(String phone) {
        this.phone = phone;
    }


    public String getPassword() {
        return password;
    }


    public void setPassword(String password) {
        this.password = password;
    }


    public String getRegisterCode() {
        return registerCode;
    }


    public void setRegisterCode(String registerCode) {
        this.registerCode = registerCode;
    }

    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPwd() {
        return pwd;
    }
    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
    
    @Override
    public String execute() throws Exception {
        // TODO Auto-generated method stub
        return null;
    }

}
